<?php
class ControllerMenuFood extends Controller {
	private $error = array();

	public function index() {
		$this->load->language('common/header');
		$this->load->language('menu/food');
			
		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('menu/food');

		$this->getList();
	}

	public function insert() {
		$this->load->language('common/header');
		$this->load->language('menu/food');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('menu/food');
		$this->load->model('menu/menu');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_menu_food->addFood($this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('menu/food', 'token=' . $this->session->data['token'] . $this->url(), 'SSL'));
		}

		$this->getForm();
	}

	public function update() {
		$this->load->language('common/header');
		$this->load->language('menu/food');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('menu/food');
		$this->load->model('menu/menu');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_menu_food->editFood($this->request->get['food_id'], $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('menu/food', 'token=' . $this->session->data['token'] . $this->url(), 'SSL'));
		}

		$this->getForm();
	}

	public function delete() {
		$this->load->language('common/header');
		$this->load->language('menu/food');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('menu/food');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $food_id) {
				$this->model_menu_food->deleteFood($food_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('menu/food', 'token=' . $this->session->data['token'] . $this->url(), 'SSL'));
		}

		$this->getList();
	}

	private function getList() {
		if (isset($this->request->get['filter_menu'])) {
			$filter_menu = $this->request->get['filter_menu'];
		} else {
			$filter_menu = '';
		}
		
		if (isset($this->request->get['filter_name'])) {
			$filter_name = $this->request->get['filter_name'];
		} else {
			$filter_name = null;
		}
		
		if (isset($this->request->get['filter_price'])) {
			$filter_price = $this->request->get['filter_price'];
		} else {
			$filter_price = null;
		}

		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
		} else {
			$filter_status = null;
		}

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
		);
		
		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_restaurant'),
			'href'      => '#',
      		'separator' => ' :: '
		);
		
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_menu'),
			'href'      => '#',
			'separator' => ' :: '
		);
				
		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => '#',       		
      		'separator' => ' :: '
		);

		$this->data['insert'] = $this->url->link('menu/food/insert', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');
		$this->data['delete'] = $this->url->link('menu/food/delete', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');
		 
		$this->data['foods'] = array();

		$page = isset($this->request->get['page']) ? $this->request->get['page'] : 1;
		$sort = isset($this->request->get['sort']) ? $this->request->get['sort'] : 'fd.name';
		$order = isset($this->request->get['order']) ? $this->request->get['order'] : 'ASC';

		$this->data['foods'] = array();
		
		$data = array(
			'filter_menu' => $filter_menu, 
			'filter_name'	  => $filter_name, 
			'filter_price'	  => $filter_price,
			'filter_status'   => $filter_status,
			'sort'            => $sort,
			'order'           => $order,
			'start'           => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'           => $this->config->get('config_admin_limit')
		);
		
		$this->load->model('menu/menu');
		$this->data['menus'] = array();
		$this->data['menus'] = $this->model_menu_menu->getMenusByParentId(0);
		
		$this->load->model('setting/image');

		$food_total = $this->model_menu_food->getTotalFoods($data);
		 
		$results = $this->model_menu_food->getFoods($data);

		$this->load->model('restaurant/restaurant');

		foreach ($results as $result) {
			$action = array();
			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('menu/food/update', 'token=' . $this->session->data['token'] . '&food_id=' . $result['food_id'] . $this->url(), 'SSL')
			);
			
			if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
				$image = $this->model_setting_image->resize($result['image'], 40, 40);
			} else {
				$image = $this->model_setting_image->resize('no_image.jpg', 40, 40);
			}
	
			$special = false;
			
			$food_specials = $this->model_menu_food->getFoodSpecials($result['food_id']);
			
			foreach ($food_specials  as $food_special) {
				if (($food_special['date_start'] == '0000-00-00' || $food_special['date_start'] > date('Y-f-d')) && ($food_special['date_end'] == '0000-00-00' || $food_special['date_end'] < date('Y-f-d'))) {
					$special = $food_special['price'];
			
					break;
				}					
			}
	
      		$this->data['foods'][] = array(
				'food_id' 	 => $result['food_id'],
      			'name'       => $result['name'],
				'price'      => $result['price'],
				'special'    => $special,
				'image'      => $image,
      			'sort_order' => $result['sort_order'],
				'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'selected'   => isset($this->request->post['selected']) && in_array($result['food_id'], $this->request->post['selected']),
				'action'     => $action
			);
		}

		// Mapping all language variables
		$this->data = array_merge($this->data, $this->language->getData());
		 
		$this->data['token'] = $this->session->data['token'];

		if (!isset($this->error['warning'])) {
			$this->data['error_warning'] = '';
		}
			
		if (isset($this->error['menu_in_use'])) {
			$this->data['error_menu_in_use'] = $this->error['menu_in_use'];
		} else {
			$this->data['error_menu_in_use'] = array();
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$this->data['sort_name'] = $this->url->link('menu/food', 'token=' . $this->session->data['token'] . $this->url('fd.name'), 'SSL');
		$this->data['sort_price'] = $this->url->link('menu/food', 'token=' . $this->session->data['token'] . $this->url('f.price'), 'SSL');
		$this->data['sort_sort_order'] = $this->url->link('menu/food', 'token=' . $this->session->data['token'] . $this->url('f.sort_order'), 'SSL');
		$this->data['sort_status'] = $this->url->link('menu/food', 'token=' . $this->session->data['token'] . $this->url('f.status'), 'SSL');

		$pagination = new Pagination();
		$pagination->total = $food_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('menu/food', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');
		 
		$this->data['pagination'] = $pagination->render();
				
		$this->data['filter_menu'] = $filter_menu;
		$this->data['filter_name'] = $filter_name;
		$this->data['filter_price'] = $filter_price;
		$this->data['filter_status'] = $filter_status;

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'menu/food_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	private function getForm() {
		// Mapping all language variables
		$this->data = array_merge($this->data, $this->language->getData());
			
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
	
 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = array();
		}

 		if (isset($this->error['menu'])) {
			$this->data['error_menu'] = $this->error['menu'];
		} else {
			$this->data['error_menu'] = '';
		}
		
  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
   			'text'      => $this->language->get('text_restaurant'),
   			'href'      => '#',
   			'separator' => ' :: '
   		);
   				
   		$this->data['breadcrumbs'][] = array(
   			'text'      => $this->language->get('text_menu'),
   			'href'      => '#',
			'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('menu/food', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		if (!isset($this->request->get['food_id'])) {
   			$this->data['breadcrumbs'][] = array(
	       		'text'      => $this->language->get('heading_title_insert'),
				'href'      => '#',
	      		'separator' => ' :: '
			);
   		} else {
   			$this->data['breadcrumbs'][] = array(
	       		'text'      => $this->language->get('heading_title_update'),
				'href'      => '#',
	      		'separator' => ' :: '
			);
   		}
		
		if (!isset($this->request->get['food_id'])) {
			$this->data['action'] = $this->url->link('menu/food/insert', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');
		} else {
			$this->data['action'] = $this->url->link('menu/food/update', 'token=' . $this->session->data['token'] . '&food_id=' . $this->request->get['food_id'] . $this->url(), 'SSL');
		}
		
		$this->data['cancel'] = $this->url->link('menu/food', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');

		$this->data['token'] = $this->session->data['token'];

		if (isset($this->request->get['food_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$food_info = $this->model_menu_food->getFood($this->request->get['food_id']);
    	}
		
		$this->load->model('setting/language');
		
		$this->data['languages'] = $this->model_setting_language->getLanguages(array('supported' => $this->restaurant->getConfig()->get('config_supported_language')));

		if (isset($this->request->post['food_description'])) {
			$this->data['food_description'] = $this->request->post['food_description'];
		} elseif (isset($this->request->get['food_id'])) {
			$this->data['food_description'] = $this->model_menu_food->getFoodDescriptions($this->request->get['food_id']);
		} else {
			$this->data['food_description'] = array();
		}

		$menus = $this->model_menu_menu->getMenusByParentId(0);

		$this->data['menus'] = $menus;

		if (isset($this->request->post['food_menu'])) {
			$this->data['food_menu'] = $this->request->post['food_menu'];
		} elseif (isset($this->request->get['food_id'])) {
			$this->data['food_menu'] = $this->model_menu_food->getFoodMenus($this->request->get['food_id']);
		} else {
			$this->data['food_menu'] = array();
		}
	
    	if (isset($this->request->post['shipping'])) {
      		$this->data['shipping'] = $this->request->post['shipping'];
    	} elseif (!empty($food_info)) {
      		$this->data['shipping'] = $food_info['shipping'];
    	} else {
			$this->data['shipping'] = 0;
		}
		
		if (isset($this->request->post['price'])) {
			$this->data['price'] = $this->request->post['price'];
		} elseif (!empty($food_info)) {
			$this->data['price'] = $food_info['price'];
		} else {
			$this->data['price'] = '';
		}
		
		if (isset($this->request->post['keyword'])) {
			$this->data['keyword'] = $this->request->post['keyword'];
		} elseif (!empty($food_info)) {
			$this->data['keyword'] = $food_info['keyword'];
		} else {
			$this->data['keyword'] = '';
		}
	
		if (isset($this->request->post['food_tag'])) {
			$this->data['food_tag'] = $this->request->post['food_tag'];
		} elseif (isset($this->request->get['food_id'])) {
			$this->data['food_tag'] = $this->model_menu_food->getFoodTags($this->request->get['food_id']);
		} else {
			$this->data['food_tag'] = array();
		}

		if (isset($this->request->post['image'])) {
			$this->data['image'] = $this->request->post['image'];
		} elseif (!empty($food_info)) {
			$this->data['image'] = $food_info['image'];
		} else {
			$this->data['image'] = '';
		}
		
		$this->load->model('setting/image');

		if (!empty($this->data['image']) && file_exists(DIR_IMAGE . $this->data['image'])) {
			$this->data['thumb'] = $this->model_setting_image->resize($this->data['image'], 100, 100);
		} else {
			$this->data['thumb'] = $this->model_setting_image->resize('no_image.jpg', 100, 100);
		}
		
		$this->data['no_image'] = $this->model_setting_image->resize('no_image.jpg', 100, 100);
		
		if (isset($this->request->post['top'])) {
			$this->data['top'] = $this->request->post['top'];
		} elseif (!empty($food_info)) {
			$this->data['top'] = $food_info['top'];
		} else {
			$this->data['top'] = 0;
		}
				
		if (isset($this->request->post['sort_order'])) {
			$this->data['sort_order'] = $this->request->post['sort_order'];
		} elseif (!empty($food_info)) {
			$this->data['sort_order'] = $food_info['sort_order'];
		} else {
			$this->data['sort_order'] = 0;
		}
		
		if (isset($this->request->post['status'])) {
			$this->data['status'] = $this->request->post['status'];
		} elseif (!empty($food_info)) {
			$this->data['status'] = $food_info['status'];
		} else {
			$this->data['status'] = 1;
		}
		
		if (isset($this->request->post['food_attribute'])) {
			$this->data['food_attributes'] = $this->request->post['food_attribute'];
		} elseif (isset($this->request->get['food_id'])) {
			$this->data['food_attributes'] = $this->model_menu_food->getFoodAttributes($this->request->get['food_id']);
		} else {
			$this->data['food_attributes'] = array();
		}
		
		if (isset($this->request->post['food_option'])) {
			$food_options = $this->request->post['food_option'];
		} elseif (isset($this->request->get['food_id'])) {
			$food_options = $this->model_menu_food->getFoodOptions($this->request->get['food_id']);			
		} else {
			$food_options = array();
		}
		
		$this->data['food_options'] = array();
			
		foreach ($food_options as $food_option) {
			if ($food_option['type'] == 'select' || $food_option['type'] == 'radio' || $food_option['type'] == 'checkbox' || $food_option['type'] == 'image') {
				$food_option_value_data = array();
				
				if(isset($food_option['food_option_value'])){
					foreach ($food_option['food_option_value'] as $food_option_value) {
						$food_option_value_data[] = array(
							'food_option_value_id' => $food_option_value['food_option_value_id'],
							'option_value_id'         => $food_option_value['option_value_id'],
							'price'                   => $food_option_value['price'],
							'price_prefix'            => $food_option_value['price_prefix']
						);						
					}
				}
				
				$this->data['food_options'][] = array(
					'food_option_id'    => $food_option['food_option_id'],
					'option_id'            => $food_option['option_id'],
					'name'                 => $food_option['name'],
					'type'                 => $food_option['type'],
					'food_option_value'    => $food_option_value_data,
					'required'             => $food_option['required']
				);				
			} else {
				$this->data['food_options'][] = array(
					'food_option_id' => $food_option['food_option_id'],
					'option_id'         => $food_option['option_id'],
					'name'              => $food_option['name'],
					'type'              => $food_option['type'],
					'option_value'      => $food_option['option_value'],
					'required'          => $food_option['required']
				);				
			}
		}
		
		if (isset($this->request->post['food_special'])) {
			$this->data['food_specials'] = $this->request->post['food_special'];
		} elseif (isset($this->request->get['food_id'])) {
			$this->data['food_specials'] = $this->model_menu_food->getFoodSpecials($this->request->get['food_id']);
		} else {
			$this->data['food_specials'] = array();
		}
				
		if (isset($this->request->post['food_image'])) {
			$food_images = $this->request->post['food_image'];
		} elseif (isset($this->request->get['food_id'])) {
			$food_images = $this->model_menu_food->getFoodImages($this->request->get['food_id']);
		} else {
			$food_images = array();
		}
		
		$this->data['food_images'] = array();
		
		foreach ($food_images as $food_image) {
			if ($food_image['image'] && file_exists(DIR_IMAGE . $food_image['image'])) {
				$image = $food_image['image'];
			} else {
				$image = 'no_image.jpg';
			}
			
			$this->data['food_images'][] = array(
				'image'      => $image,
				'thumb'      => $this->model_setting_image->resize($image, 100, 100),
				'sort_order' => $food_image['sort_order'],
			);
		}

		$this->data['no_image'] = $this->model_setting_image->resize('no_image.jpg', 100, 100);
		
		$this->load->model('menu/option');
		$data = array(
			'group' => 'food'
		);
		$this->data['options'] = $this->model_menu_option->getOptions($data);

		$this->template = 'menu/food_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'menu/food')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		foreach ($this->request->post['food_description'] as $language_id => $value) {
			if ((utf8_strlen($value['name']) < 2) || (utf8_strlen($value['name']) > 255)) {
				$this->error['name'][$language_id] = $this->language->get('error_name');
			}
		}
		
		if (!isset($this->request->post['food_menu'])) {
			$this->error['menu'] = $this->language->get('error_menu');
		}
		
		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}
					
		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

  	private function validateDelete() {
    	if (!$this->user->hasPermission('modify', 'menu/food')) {
      		$this->error['warning'] = $this->language->get('error_permission');  
    	}
		
		if (!$this->error) {
	  		return true;
		} else {
	  		return false;
		}
  	}

	private function url($sort = ''){
		$url = "";
			
		if (isset($this->request->get['filter_menu'])) {
			$url .= '&filter_menu=' . $this->request->get['filter_menu'];
		}
				
		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . $this->request->get['filter_name'];
		}
		
		if (isset($this->request->get['filter_price'])) {
			$url .= '&filter_price=' . $this->request->get['filter_price'];
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		if(!empty($sort)) {
			if ($order == 'ASC') {
				$url .= '&order=DESC';
			} else {
				$url .= '&order=ASC';
			}

			$url .= '&sort=' . $sort;
		} else {

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			} else {
				$url .= '&sort=fd.name';
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
		}

		$url .= '&page=' . $page;

		return $url;
	}
}
?>